﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Linq.Extensions;
using Abp.UI;
using AutoMapper;
using MyCompanyName.AbpZeroTemplate.Authorization;
using MyCompanyName.AbpZeroTemplate.CategoryApp.Dto;
using MyCompanyName.AbpZeroTemplate.Entities;
using MyCompanyName.AbpZeroTemplate.IRepositories;

namespace MyCompanyName.AbpZeroTemplate.CategoryApp
{
    [AbpAuthorize(AppPermissions.Pages_Category)]
    public class CategoryAppService : AbpZeroTemplateAppServiceBase, ICategoryAppService
    {
        private readonly ICategoryRepository _categoryRepository;
        public CategoryAppService(ICategoryRepository categoryRepository)
        {
            _categoryRepository = categoryRepository;
        }

        [AbpAuthorize(AppPermissions.Pages_Category_Create)]
        public void CreateCategory(CreateCategoryInput input)
        {
            int count=_categoryRepository.Count(a => a.Name.Equals(input.Name));
            if (count > 0)
            {
                throw new UserFriendlyException("分类名称已存在！");
            }
            _categoryRepository.Insert(new Category()
            {
                Name = input.Name
            });
        }

        [AbpAuthorize(AppPermissions.Pages_Category_Delete)]
        public void DeleteCategory(EntityRequestInput input)
        {
            var category=_categoryRepository.Get(input.Id);
            _categoryRepository.Delete(category);
        }

        public PagedResultOutput<CategoryOutput> GetCategories(GetCategoriesInput input)
        {
            //创建映射
            Mapper.CreateMap<Category, CategoryOutput>();
            var result=_categoryRepository.GetAll();
            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                result=result.Where(a => a.Name.Contains(input.Filter));
            }
            
            int totalCount = result.Count();
            var list=result.OrderBy(input.Sorting).PageBy(input).ToList();//分页
            return new PagedResultOutput<CategoryOutput>(
                totalCount,
                Mapper.Map<List<CategoryOutput>>(list)
                );

        }

        public CategoryOutput GetCategoryForEdit(EntityRequestInput input)
        {
            var category = _categoryRepository.Get(input.Id);
            return new CategoryOutput()
            {
                Id = category.Id,
                Name = category.Name
            };
        }

        [AbpAuthorize(AppPermissions.Pages_Category_Edit)]
        public void UpdateCategory(CreateCategoryInput input)
        {
            int count = _categoryRepository.Count(a => a.Name.Equals(input.Name) && a.Id!=input.Id);
            if (count > 0)
            {
                throw new UserFriendlyException("分类名称已存在！");
            }
            var category=_categoryRepository.Get(input.Id);
            category.Name = input.Name;
        }

        public List<CategoryOutput> GetCategoriesToList()
        {
            //创建映射
            Mapper.CreateMap<Category, CategoryOutput>();
            var result = _categoryRepository.GetAll();
            
            var list = result.ToList();
            return Mapper.Map<List<CategoryOutput>>(list);

        }
    }
}
